const BaseController = require("./base/base");
const Result = require("../utils/result");
const tools = require("../utils/tools");
const cartModel = require("../model/cart");
const { getTokenUser } = require("../middlewares/auth");
class CartController extends BaseController {
  constructor() {
    super();
  }
  findByUserId(req, res, next) {
    setTimeout(async ()=>{
      try { 
        const { userid } = req.params;
        let { user_id } = getTokenUser(req);
        if (user_id != userid) return res.json(Result.fail("用户ID有误,数据篡改"));
        let ret = await cartModel.findByUserId(userid);
        res.json(Result.ok("success", ret));
      }catch (err) {
        res.json(Result.error(err));
      }
    })
  }
  insert(req, res, next) {
    setTimeout(async ()=>{
      try {
        const { user_id, goods_id, goods_num  } = req.body;
        if (!user_id || !goods_id || !goods_num) return res.json(Result.fail("user_id, goods_id, goods_num 参数不能为空"));
        
        let { user_id: userid } = getTokenUser(req);
        if (user_id != userid) return res.json(Result.fail("用户ID有误,数据篡改"));

        let cart = await cartModel.select(user_id,  goods_id); //有数据修改,没数据就插入
        if(cart && cart.length == 0){
          let ret = await cartModel.insert({ user_id, goods_id, goods_num, add_time: tools.getNowTime() });
          !ret.insertId ?
            res.json(Result.fail("添加失败", ret)) :
            res.json(Result.ok("添加成功", ret));
        }else{
          let goods_num = ++cart[0].goods_num;
          let ret = await cartModel.update({goods_num}, user_id, cart[0].cart_id);
          !ret.affectedRows ?
          res.json(Result.fail("修改失败", ret)) :
          res.json(Result.ok("修改成功", ret));
        }
      } catch (err) {
        res.json(Result.error(err));
      }
    })
  }
  update(req, res, next){
    setTimeout(async ()=>{
      try {
        const { id } = req.params;
        const { user_id, goods_num  } = req.body;
        if (!user_id || !goods_num) return res.json(Result.fail("user_id, goods_num 参数不能为空"));

        let { user_id: userid } = getTokenUser(req);
        if (user_id != userid) return res.json(Result.fail("用户ID有误,数据篡改"));

        let ret = await cartModel.update({goods_num}, user_id, id);
        !ret.affectedRows ?
          res.json(Result.fail("修改失败", ret)) :
          res.json(Result.ok("修改成功", ret));
      } catch (err) {
        res.json(Result.error(err));
      }
    })
  }
  delById(req, res, next){
    setTimeout(async ()=>{
      try {
        const { userid } = req.params;  //用户ID
        const { goods_id  } = req.query; //商品ID

        let { user_id } = getTokenUser(req);
        if (userid != user_id) return res.json(Result.fail("用户ID有误,数据篡改"));

        let ret = await cartModel.delById(user_id, goods_id);
        !ret.affectedRows ?
          res.json(Result.fail("删除失败", ret)) :
          res.json(Result.ok("删除成功", ret));
      } catch (err) {
        res.json(Result.error(err));
      }
    })
  }
}
module.exports = new CartController().resolve();